package com.android.djlmapp.activity.home;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.android.djlmapp.R;
import com.android.djlmapp.base.BaseActivity;
import com.android.djlmapp.base.BaseFragment;
import com.android.djlmapp.fragment.DingDanLieBiaoFragment;
import com.android.djlmapp.fragment.GeRenZhongXinFragment;
import com.android.djlmapp.fragment.ShouYeFragment;
import com.android.djlmapp.model.my.UpdateInfo;
import com.android.djlmapp.tools.global.KeyBordUtils;
import com.android.readcard.eventbus.Event;
import com.android.readcard.eventbus.EventCodes;
import com.flyco.tablayout.CommonTabLayout;
import com.flyco.tablayout.listener.CustomTabEntity;
import com.flyco.tablayout.listener.OnTabSelectListener;
import com.meiqia.core.MQManager;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;

public class HomeActivity extends BaseActivity implements OnTabSelectListener {
    private static final String[] HOME_TABS = {"首页", "订单", "我的"};
    private static final int[] HOME_SelectedICONS = {
            R.mipmap.select_home, R.mipmap.select_order,
            R.mipmap.select_my_icon};
    private static final int[] HOME_UNSelectedICONS = {
            R.mipmap.unselect_home, R.mipmap.unselect_order,
            R.mipmap.unselect_my_icon};

    public ArrayList<CustomTabEntity> mTabEntities;

    @BindView(R.id.home_tl)
    CommonTabLayout mTabLayout;
    private BaseFragment[] fragments = new BaseFragment[3];
    private BaseFragment currentFragment = null;

    private boolean isExit = false;
    Handler mHandler = new Handler() {

        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            isExit = false;
        }

    };

    @BindView(R.id.home_bottom)
    LinearLayout homeBottom;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
        initView();
        initData();
    }

    @Override
    protected void onResume() {
        super.onResume();
        checkUpdate();
    }

    @Override
    protected void initView() {

        mTabEntities = new ArrayList<>();
        for (int i = 0; i < HOME_TABS.length; i++) {
            mTabEntities.add(new TabEntity(HOME_TABS[i], HOME_SelectedICONS[i], HOME_UNSelectedICONS[i]));
        }

        mTabLayout.setTabData(mTabEntities);
        mTabLayout.setOnTabSelectListener(this);
        KeyBordUtils.addOnSoftKeyBoardListener(this, new KeyBordUtils.OnSoftKeyBoardListener() {
            @Override
            public void keyBoardShow() {
                //软键盘弹出，隐藏底部控件
                homeBottom.setVisibility(View.GONE);
            }

            @Override
            public void keyBoardHide() {
                //软键盘消失，显示底部控件
                homeBottom.setVisibility(View.VISIBLE);
            }
        });
    }

    @Override
    protected void initData() {
        fragments = new BaseFragment[HOME_TABS.length];
        ShouYeFragment homeFragment = findFragment(ShouYeFragment.class);
        if (homeFragment == null) {
            homeFragment = ShouYeFragment.newInstance(ShouYeFragment.class.getName());
            DingDanLieBiaoFragment dingDanLieBiaoFragment = DingDanLieBiaoFragment.newInstance(DingDanLieBiaoFragment.class.getName());
            GeRenZhongXinFragment geRenZhongXinFragment = GeRenZhongXinFragment.newInstance(GeRenZhongXinFragment.class.getName());
            fragments[0] = homeFragment;
            fragments[1] = dingDanLieBiaoFragment;
            fragments[2] = geRenZhongXinFragment;
            loadMultipleRootFragment(R.id.home_tab, 0, fragments);
        } else {
            DingDanLieBiaoFragment dingDanLieBiaoFragment = findFragment(DingDanLieBiaoFragment.class);
            if (dingDanLieBiaoFragment == null) {
                dingDanLieBiaoFragment = DingDanLieBiaoFragment.newInstance(DingDanLieBiaoFragment.class.getName());
            }
            GeRenZhongXinFragment geRenZhongXinFragment = findFragment(GeRenZhongXinFragment.class);
            if (geRenZhongXinFragment == null) {
                geRenZhongXinFragment = GeRenZhongXinFragment.newInstance(GeRenZhongXinFragment.class.getName());
            }
            fragments[0] = homeFragment;
            fragments[1] = dingDanLieBiaoFragment;
            fragments[2] = geRenZhongXinFragment;
        }
        currentFragment = fragments[0];

    }

    @Override
    protected boolean isRegisterEventBus() {
        // 在需要跳转订单的地方发送消息  EventBusUtil.sendEvent(new Event<>(EventCodes.EventHomeCode.A));
        return true;
    }

    @Override
    protected void receiveEvent(Event event) {
        // "tabB"
        switch (event.getCode()) {
            case EventCodes.EventHomeCode.A:
//                startActivity(new Intent(HomeActivity.this, OrderListActivity.class));
                break;
        }
    }

    @Override
    public void onTabSelect(int position) {
        showHideFragment(fragments[position], currentFragment);
        currentFragment = fragments[position];
        if (position == 0) {
            checkUpdate();
        }
    }

    @Override
    public void onTabReselect(int position) {

    }

    public class TabEntity implements CustomTabEntity {
        public String title;
        public int selectedIcon;
        public int unSelectedIcon;

        public TabEntity(String title, int selectedIcon, int unSelectedIcon) {
            this.title = title;
            this.selectedIcon = selectedIcon;
            this.unSelectedIcon = unSelectedIcon;
        }

        @Override
        public String getTabTitle() {
            return title;
        }

        @Override
        public int getTabSelectedIcon() {
            return selectedIcon;
        }

        @Override
        public int getTabUnselectedIcon() {
            return unSelectedIcon;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        currentFragment.onActivityResult(requestCode, resultCode, data);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN) { //启动一个意图,回到桌面
            MQManager.getInstance(this).setClientOffline();
            //            Intent backHome = new Intent(Intent.ACTION_MAIN);
            //            backHome.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            //            backHome.addCategory(Intent.CATEGORY_HOME);
            //            startActivity(backHome);
            moveTaskToBack(true);
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    public void exit() {
        if (!isExit) {
            isExit = true;
            Toast.makeText(getApplicationContext(), "再按一次退出程序", Toast.LENGTH_SHORT).show();
            mHandler.sendEmptyMessageDelayed(0, 2000);
        } else {
            //mRequestMap.remove(HomeActivity.class.getName());
            Intent intent = new Intent(Intent.ACTION_MAIN);
            intent.addCategory(Intent.CATEGORY_HOME);
            startActivity(intent);
            finish();
            System.exit(0);

        }
    }

    private void checkUpdate() {
        Log.e("项目异常", "------checkUpdate------");
        new UpdateInfo(HomeActivity.this).update();
    }
}
